package com.jxsd.edu.cn.tools;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;


/**
 * 中文分词
 * @pramati.  search用户的句子
 * */
public class Participle 
{
  public static  List<String>  getStrings(String  search)
  {
	  List<String>  list=new ArrayList<String>();
	  Analyzer analyzer = new PaodingAnalyzer(); 
	  
//	  Analyzer  analyzer2=new SmartChineseAnalyzer(Version.LUCENE_CURRENT);
	  
	  String indexString = search; 
	  StringReader reader = new StringReader(indexString); 
	  TokenStream ts = analyzer.tokenStream(indexString, reader); 
	  Token t;
	try {
		t = ts.next();
		
		while (t != null) 
		  { 
		  String  s=t.termText();
		  list.add(s);
		  t = ts.next();
		  } 
	} catch (IOException e) 
	  {
		e.printStackTrace();
	  }
	  catch(NullPointerException px)
	  {
		px.printStackTrace();  
	  }
	  catch(Exception ex)
	  {
		ex.printStackTrace();
	  }
	  
	  return  list;
  }
  
  public  static  String  getFenCiSql(List<String>  list,String  condition)
  {
	String  sql=null;
	StringBuilder  str=new  StringBuilder();
	if(list.size()==1)
	{
		str.append(condition);
		str.append("  like '%");
		str.append(list.get(0));
		str.append("%'");
	}
	else if(list.size()>1)
	{
		int  i=1;
		for(String  s : list)
		{
			if(i<list.size())
			{
				str.append(condition);
				str.append("  like '%");
				str.append(s);
				str.append("%'  or ");
			}
			else
			{
				str.append(condition);
				str.append("  like '%");
				str.append(s);
				str.append("%'");
			}
			i++;
		}
	}  
	sql=str.toString();
	return  sql;
  }
}
